package com.dyz.leetcode._16_dataStructureDesign;

import java.util.ArrayDeque;
import java.util.Queue;

public class _225_MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    public _225_MyStack() {
        this.queue1 = new ArrayDeque<>();
        this.queue2 = new ArrayDeque<>();

    }

    public void push(int x) {
        queue1.add(x);

    }

    public int pop() {
        if(!queue1.isEmpty()){
            while (!queue1.isEmpty()){
                int x = queue1.poll();
                if(queue1.isEmpty()){return x;}
                queue2.add(x);
            }
        }else if(!queue2.isEmpty()){
            while (!queue2.isEmpty()){
                int x = queue2.poll();
                if(queue2.isEmpty()){return x;}
                queue1.add(x);
            }
        }
        return -1;
    }

    public int top() {
        if(!queue1.isEmpty()){
            while (!queue1.isEmpty()){
                int x = queue1.poll();
                queue2.add(x);
                if(queue1.isEmpty()){return x;}

            }
        }else if(!queue2.isEmpty()){
            while (!queue2.isEmpty()){
                int x = queue2.poll();
                queue1.add(x);
                if(queue2.isEmpty()){return x;}
            }
        }
        return -1;
    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();

    }
}
